package cn.tianyigps.tianyigpsgatherchecker.service;

import cn.tianyigps.tianyigpsgatherchecker.bean.CheckStatusModel;
import cn.tianyigps.tianyigpsgatherchecker.bean.EquipmentCheckModel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@Service
public class CheckPushAlarm {
    @Autowired
    FindCarAllEquIds findCarAllEquIds;
    @Autowired
    EquipmentCheckModelService equipmentCheckModelService;
    private final  static  String SUCCESS = "200";
    /**
     * @author jiangkai
     * @Description: 返回equId车辆报警过,cleanAlarm为true关闭报警
     */
    public String checkPushAlarm(EquipmentCheckModel equipmentCheckModel, String ruleId, boolean cleanAlarm) throws IOException {
        String rest = findCarAllEquIds.findCarAllEquIds(equipmentCheckModel.getEquipmentId());
        ObjectMapper mapper = new ObjectMapper();
        if (!StringUtils.isEmpty(rest)) {
            JsonNode jsonObject = mapper.readTree(rest);
            String status = jsonObject.get("status").toString();
            if (SUCCESS.equals(status)) {
                Iterator<JsonNode> jsList = jsonObject.get("data").get("items").elements();
                while (jsList.hasNext()) {
                    JsonNode node = jsList.next();
                    String equId = node.get("code").toString();
                    try {
                        EquipmentCheckModel laseEquipmentCheckModel = equipmentCheckModelService.getEquipmentCheckModelById(equId);
                        if (null != laseEquipmentCheckModel) {
                            CheckStatusModel checkStatusModel = laseEquipmentCheckModel.getCheckStatusModel();
                            if (null != checkStatusModel&&null!=checkStatusModel.getGather()) {
                                List<String> list = checkStatusModel.getGather();
                                Iterator listIterator =  list.iterator();
                                while(listIterator.hasNext())
                                {
                                    String str = (String) listIterator.next();
                                    if(ruleId.equals(str)){
                                        if(cleanAlarm){
                                            listIterator.remove();
                                            equipmentCheckModelService.updateCheckStatus(equId, list, "gather");
                                        }else {
                                            return equId;
                                        }

                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}
